package com.weijianhuawen.wjblog.web.service.impl;

import com.weijianhuawen.wjblog.common.enums.ResponseCodeEnum;
import com.weijianhuawen.wjblog.common.exception.BizException;
import com.weijianhuawen.wjblog.common.utils.MinioUtil;
import com.weijianhuawen.wjblog.common.utils.Response;
import com.weijianhuawen.wjblog.web.mode.vo.file.UploadFileRspVo;
import com.weijianhuawen.wjblog.web.service.FileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * @version: java version 8
 * @Author: weijianhuawen
 * @description:
 * @date: 2024-12-12 0:49
 */

@Service
@Slf4j
public class FileServiceImpl implements FileService {
    @Autowired
    private MinioUtil minioUtil;
    @Override
    public Response uploadFile(MultipartFile file) {
        try {
            // 将文件上传到Minio服务
            String url = minioUtil.uploadFile(file);

            // 返回文件上传url
            return Response.success(UploadFileRspVo.builder().url(url).build());
        } catch (BizException e) {
            log.warn("【前台文件上传服务】 文件上传出现业务异常，异常原因：{}", e.getMessage());
            throw e;
        } catch (Exception e) {
            log.error("【前台文件服务】 文件上传出现未知错误：{}", e.getMessage());
            throw new BizException(ResponseCodeEnum.UPLOAD_FILE_UNKNOW_ERROR);
        }
    }
}
